/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedom.railhunter.ui;

import java.util.concurrent.ExecutionException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import org.freedom.railhunter.service.CookieService;
import org.freedom.railhunter.service.RandCodeService;

/**
 *
 * @author Alan
 */
public class RandCodeDialog extends javax.swing.JDialog {
    
    private String randCode;

    /**
     * Creates new form RandCodeDialog
     */
    public RandCodeDialog(java.awt.Frame parent, boolean modal, byte[] imgBytes) {
        super(parent, modal);
        initComponents();
        codeImageLabel.setIcon(new ImageIcon(imgBytes));
    }

    public String getRandCode() {
        return randCode;
    }

    public void setRandCode(String randCode) {
        this.randCode = randCode;
    }
    
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        codeImageLabel = new javax.swing.JLabel();
        randcodeField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        codeImageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        codeImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/busy.png"))); // NOI18N
        codeImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                codeImageLabelMouseClicked(evt);
            }
        });

        randcodeField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                randcodeFieldActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jLabel1.setText("请输入验证码：");

        cancelButton.setText("取消");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("确定");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(codeImageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(randcodeField, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(codeImageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(randcodeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void codeImageLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_codeImageLabelMouseClicked
        codeImageLabel.setIcon(new ImageIcon(getClass().getResource("/img/busy.png")));
        new SwingWorker<ImageIcon, Void>() {

            @Override
            protected ImageIcon doInBackground() throws Exception {
                RandCodeService randCodeService = RandCodeService.getInstance();
                byte[] imgBytes = randCodeService.buildRandCodeImageBytes(CookieService.getInstance().getCookieData());
                return new ImageIcon(imgBytes);
            }

            @Override
            protected void done() {
                try {
                    codeImageLabel.setIcon(get());
                } catch (InterruptedException | ExecutionException ex) {
                    JOptionPane.showMessageDialog(RandCodeDialog.this, "刷新验证码失败！", "警告", JOptionPane.WARNING_MESSAGE);
                }
            }

        }.execute();
    }//GEN-LAST:event_codeImageLabelMouseClicked

    private void fillRandCode() {
        setRandCode(randcodeField.getText());
        this.setVisible(false);
        this.dispose();
    }
    
    private void randcodeFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randcodeFieldActionPerformed
        fillRandCode();
    }//GEN-LAST:event_randcodeFieldActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        fillRandCode();
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel codeImageLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField randcodeField;
    // End of variables declaration//GEN-END:variables
}
